package com.jprocms.module.cms.config;

import com.jprocms.module.cms.framework.seq.SnowflakeSequence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.jprocms.module.cms.framework.seq.SnowflakeSequence.MAX_DATACENTER_ID;
import static com.jprocms.module.cms.framework.seq.SnowflakeSequence.MAX_WORKER_ID;


@Configuration
public class SnowflakeSequenceConfig {
    /**
     * 雪花算法ID生成器
     */
    @Bean
    public SnowflakeSequence snowflakeSequence(JproProperties props) {
        int datacenterId = props.getDatacenterId();
        int workerId = props.getWorkerId();
        if (datacenterId >= 0 && datacenterId <= MAX_DATACENTER_ID && workerId >= 0 && workerId <= MAX_WORKER_ID) {
            return new SnowflakeSequence(datacenterId, workerId);
        }
        return new SnowflakeSequence(null);
    }
}
